import pygame, sys, math

class Player():
    def __init__(self, originalimage, shieldimage, pos =(0,0), size = (50,50)):
        self.image = pygame.image.load(originalimage)
        self.originalimage = pygame.image.load(originalimage)
        self.shieldimage = pygame.image.load(shieldimage)
        self.rect = self.image.get_rect()
        self.place(pos)
        self.fastSpeed = 5
        self.normalSpeed = 3
        self.maxSpeedx = self.normalSpeed
        self.maxSpeedy = self.normalSpeed
        self.speed = [0,0]
        self.alive = True
        self.radius = self.rect.width/2
        self.didBounce = False
        self.speedUpTimerMax = 60*5
        self.speedUpTimer = self.speedUpTimerMax
        self.shieldUp = False
        
    def move(self):
        self.rect = self.rect.move(self.speed)
        
    def update(self):
        self.move()
        
        if 0 < self.speedUpTimer < self.speedUpTimerMax:
            self.speedUpTimer -= 1
        elif self.speedUpTimer == 0:
            self.maxSpeedx = self.normalSpeed
            self.maxSpeedy = self.normalSpeed
            self.speedUpTimer = self.speedUpTimerMax

    def direction(self, dir):
        if dir == "right":
            self.speed[0] = self.maxSpeedx
        if dir == "left":
            self.speed[0] = -self.maxSpeedx   
        if dir == "stop right":
            self.speed[0] = 0  
        if dir == "stop left":
            self.speed[0] = 0   
        if dir == "up":
            self.speed[1] = -self.maxSpeedy
        if dir == "down":
            self.speed[1] = self.maxSpeedy
        if dir == "stop up":
            self.speed[1] = 0  
        if dir == "stop down":
            self.speed[1] = 0
        print self.speed
            
    def speedUp(self):
        self.maxSpeedx = self.fastSpeed
        self.maxSpeedy = self.fastSpeed
        self.speedUpTimer -= 1
        
    def shield(self):
        self.image = self.shieldimage
        self.shieldUp = True
        
    def place(self, pt):
        self.rect.center = pt
        
    def outerwallhit(self, width, height):
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0] = 0
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speed[1] = 0
    
    def enemyhit(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.distanceToPoint(other.rect.center) < (self.radius + other.radius):
                    print "????????????????", self.shieldUp
                    if self.shieldUp:
                        print "-------------"
                        other.speedx = -other.speedx
                        other.speedy = -other.speedy
                        self.didBounce = True
                        self.shieldUp = False
                        self.image = self.originalimage
                    else:
                        self.alive = False
                        
    
    def distanceToPoint(self, pt):
        x1 = self.rect.center[0]
        y1 = self.rect.center[1]
        x2 = pt[0]
        y2 = pt[1]
        
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
        
    def wallhit(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.distanceToPoint(other.rect.center) < (self.radius + other.radius):
                        self.speed[0] = 0
                        self.speed[1] = 0
